package com.ruoyi.report_lqn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqn.domain.bo.ReportLqnRhlqbgBsInfoBo;
import com.ruoyi.report_lqn.domain.vo.ReportLqnRhlqbgBsInfoVo;
import com.ruoyi.report_lqn.domain.ReportLqnRhlqbgBsInfo;
import com.ruoyi.report_lqn.mapper.ReportLqnRhlqbgBsInfoMapper;
import com.ruoyi.report_lqn.service.IReportLqnRhlqbgBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 道路用乳化沥青试验检测报告Service业务层处理
 *
 * @author tll
 * @date 2024-09-12
 */
@RequiredArgsConstructor
@Service
public class ReportLqnRhlqbgBsInfoServiceImpl implements IReportLqnRhlqbgBsInfoService {

    private final ReportLqnRhlqbgBsInfoMapper baseMapper;

    private final TgsybgBaDetailMapper detailMapper;

    private static final Long[] keys = {198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L, 209L, 210L, 211L};

    private final IBiQualificationService biQualificationService;

    /**
     * 查询道路用乳化沥青试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportLqnRhlqbgBsInfoVo queryById(Long excelId) {
        // 主表
        ReportLqnRhlqbgBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId).orderByAsc(TgsybgBaDetail::getId));
        baDetailVos.forEach(v -> {
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        // 转换成map形式
        Map<Long, TgsybgBaDetailVo> collect = baDetailVos.stream().collect(Collectors.toMap(TgsybgBaDetailVo::getQualId, v -> {
            return v;
        }));
        if (CollUtil.isNotEmpty(collect)) {
            vo.setBaDetailVos(collect);
        }
        return vo;
    }

    /**
     * 查询道路用乳化沥青试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportLqnRhlqbgBsInfoVo> queryPageList(ReportLqnRhlqbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqnRhlqbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqnRhlqbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询道路用乳化沥青试验检测报告列表
     */
    @Override
    public List<ReportLqnRhlqbgBsInfoVo> queryList(ReportLqnRhlqbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqnRhlqbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqnRhlqbgBsInfo> buildQueryWrapper(ReportLqnRhlqbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqnRhlqbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqnRhlqbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqnRhlqbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqnRhlqbgBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqnRhlqbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqnRhlqbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqnRhlqbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqnRhlqbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqnRhlqbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqnRhlqbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportLqnRhlqbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqnRhlqbgBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqnRhlqbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqnRhlqbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqnRhlqbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqnRhlqbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqnRhlqbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqnRhlqbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportLqnRhlqbgBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportLqnRhlqbgBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqnRhlqbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), ReportLqnRhlqbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), ReportLqnRhlqbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportLqnRhlqbgBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportLqnRhlqbgBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportLqnRhlqbgBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportLqnRhlqbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getRefExcelId() != null, ReportLqnRhlqbgBsInfo::getRefExcelId, bo.getRefExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqnRhlqbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportLqnRhlqbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqnRhlqbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqnRhlqbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportLqnRhlqbgBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), ReportLqnRhlqbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitbh()), ReportLqnRhlqbgBsInfo::getWeitbh, bo.getWeitbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJianclb()), ReportLqnRhlqbgBsInfo::getJianclb, bo.getJianclb());
        lqw.eq(StringUtils.isNotBlank(bo.getFenlpzdh()), ReportLqnRhlqbgBsInfo::getFenlpzdh, bo.getFenlpzdh());
        lqw.eq(StringUtils.isNotBlank(bo.getLqzlbh()), ReportLqnRhlqbgBsInfo::getLqzlbh, bo.getLqzlbh());
        return lqw;
    }

    /**
     * 新增道路用乳化沥青试验检测报告
     */
    @Override
    public Boolean insertByBo(ReportLqnRhlqbgBsInfoBo bo) {
        ReportLqnRhlqbgBsInfo add = BeanUtil.toBean(bo, ReportLqnRhlqbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 多个keys
//            ArrayList<TgsybgBaDetail> details = new ArrayList<>(15);
            for (Long key : keys) {
//                details.add(new TgsybgBaDetail(add.getExcelId(), key));
                detailMapper.insert(new TgsybgBaDetail(add.getExcelId(), key));
            }
//            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改道路用乳化沥青试验检测报告
     */
    @Override
    @ComputeReportResult
    public Boolean updateByBo(ReportLqnRhlqbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportLqnRhlqbgBsInfo update = BeanUtil.toBean(bo, ReportLqnRhlqbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos().values(), TgsybgBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(ReportLqnRhlqbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos().values()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqnRhlqbgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除道路用乳化沥青试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
